package com.chenyi.web.controller.api;


import com.chenyi.common.annotation.UserRequired;
import com.chenyi.common.core.domain.entity.SysUser;
import com.chenyi.common.core.domain.model.LoginUser;
import com.chenyi.common.exception.MessageException;
import com.chenyi.common.utils.ServletUtils;
import com.chenyi.framework.web.service.UserLoginService;
import com.chenyi.framework.web.service.UserTokenService;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(description = "登录模块")
@RestController
@RequestMapping("/api")
@Slf4j
public class ApiLoginController {


    @Autowired
    private UserLoginService loginService;

    @Autowired
    private UserTokenService tokenService;

    @Autowired
    private RedissonClient redissonClient;


    @ApiOperation("手机号登录")
    @PostMapping("/phoneLogin")
    @ApiImplicitParams({
            @ApiImplicitParam(name="phone",value="手机号",dataType="String", paramType = "query"),
            @ApiImplicitParam(name="password",value="密码",dataType="String", paramType = "query"),
            @ApiImplicitParam(name="userType",value="用户类型 00=普通会员",dataType="String", paramType = "query"),
            @ApiImplicitParam(name="os",value="操作系统 Androd,Ios",dataType="String", paramType = "query"),
            @ApiImplicitParam(name="browser",value="系统类型 如系统版本与系统名称",dataType="String", paramType = "query"),
    })
    public ResponseEntity<Object> phoneLogin(String phone,String password,@RequestParam(defaultValue = "00") String userType,String os,String browser){
        RLock rLock = redissonClient.getLock("API_LOGIN_" + phone);
        try {
            if (rLock == null || rLock.isLocked()){
                throw new MessageException("服务繁忙，休息一下");
            }
            rLock.lock();
            Map<String,String> data = Maps.newHashMap();
            data.put("token",loginService.phoneLogin(phone,password,userType,os,browser));
            log.info("登录成功----------手机号："+phone+",TOKEN:"+data.get("token"));
            return ResponseEntity.ok(data);
        }finally {
            rLock.unlock();
        }
    }


    /**
     * 获取用户信息
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("/getInfo")
    @UserRequired
    @ApiImplicitParams({
            @ApiImplicitParam(name = "UserAuthorization", value = "UserAuthorization", dataType = "String", paramType = "query", required = true),
    })
    public ResponseEntity<Object> getInfo(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        Map<String,Object> data = Maps.newHashMap();
        data.put("user",user);
        return ResponseEntity.ok(data);
    }


    /* 功能描述: <br>
     * 〈修改密码〉
     *
     * @param
     * @return: org.springframework.http.ResponseEntity<java.lang.Object>
     * @Author: wangkh
     * @Date: 2020/10/29 0029 14:36
     */
    @ApiOperation("获取用户信息")
    @GetMapping("/updPassword")
    @UserRequired
    @ApiImplicitParams({
            @ApiImplicitParam(name = "UserAuthorization", value = "UserAuthorization", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name="newPassword",value="新密码",dataType="String", paramType = "query"),
    })
    public ResponseEntity<Object> updPassword(String newPassword){

        return new ResponseEntity<>(HttpStatus.OK);
    }
}
